package com.takwolf.android.loadmore;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.BaseAdapter;

import com.takwolf.android.loadmore.LoadMoreListView.OnLoadMoreListener;

public class MainActivity extends Activity implements OnLoadMoreListener {

    private LoadMoreListView listView;
    private BaseAdapter adapter;
    private List<User> userList;

    private Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (LoadMoreListView) findViewById(R.id.list_view);
        userList = UserDao.getUserList();
        //userList = new ArrayList<User>();
        adapter = new UserAdapter(this, userList);
        listView.setAdapter(adapter);
        listView.setOnLoadMoreListener(this);
        listView.setAutoLoadMore(true);
    }

    boolean b = false;

    @Override
    public void onBackPressed() {
        b = !b;
        listView.setCanLoadMore(b);
    }

    @Override
    public void onLoadMore() {
        handler.postDelayed(new Runnable() {

            @Override
            public void run() {
                userList.addAll(UserDao.getUserList());
                adapter.notifyDataSetChanged();
                listView.onLoadMoreComplete();
            }

        }, 3000);
    }

}
